//
//  MTPickerTimeView.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/25.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTPickerTimeView.h"
#import "MTMaskView.h"


@interface MTPickerTimeView ()<UIPickerViewDelegate, UIPickerViewDataSource>

- (IBAction)cancleButton:(UIButton *)sender;
- (IBAction)confirmButton:(UIButton *)confirmBtn;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;

@property (nonatomic, strong) NSArray *timesArray;


@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property(nonatomic, assign) NSInteger selectIndex;
@end

@implementation MTPickerTimeView


- (NSArray *)timesArray {
    if (_timesArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"time.plist" ofType:nil];
        _timesArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _timesArray;
}
+ (instancetype)viewFromxib{
    return [[NSBundle mainBundle] loadNibNamed:@"MTPickerTimeView" owner:nil options:nil].firstObject;
}

+ (instancetype)pickerView{
    
    MTPickerTimeView *timeView = [MTPickerTimeView viewFromxib];
    [timeView showPickerView];
    
    [mainWindow addSubview:timeView];
    timeView.frame = CGRectMake(0, 400, ScreenWidth, ScreenHeight - 400);
    
    return timeView;
}




- (void)showPickerView{
    
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
   
    
}


#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 4;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    switch (component) {
        case 0:
        {
          
            return 1;
        }
            break;
        case 1:
        {
            return 24;
        }
            break;
        case 2:
        {
            return 24;
        }
            break;
        case 3:
        {
            return 1;
        }
            break;
            
        default:
            break;
    }
    return 0;
}


#pragma mark ------UIPickerViewDelegate



- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (row == 0 && component == 0) {
        return @"";
    } else if (row == 0 && component == 3){
        return @"";
    } else if (component == 1) {
        return self.timesArray[row];
    } else if (component == 2) {
        return self.timesArray[row];
    }
    return @"title";
    
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    NSString *start = [self.timesArray objectAtIndex:[self.pickerView selectedRowInComponent:1]];
    NSString *end = [self.timesArray objectAtIndex:[self.pickerView selectedRowInComponent:2]];
    
    if ([self.delegate respondsToSelector:@selector(pickerTimeView:start:end:button:)]) {
        [self.delegate pickerTimeView:self start:start end:end button:_cancleBtn];
    }
}



+ (void)setHiddenPickerTimeView:(void(^)())complent{
    
    
    for (MTPickerTimeView *timeView in mainWindow.subviews) {
        if ([timeView isKindOfClass:[MTPickerTimeView class]]) {
            
            [timeView removeFromSuperview];
            if (complent) {
                complent();
            }
        }
    }
}




- (IBAction)cancleButton:(UIButton *)sender {

    
    [self hiddenAllView];
}

- (IBAction)confirmButton:(UIButton *)sender {

    
    
    
    [self hiddenAllView];
}


- (void)hiddenAllView{
    
    [MTPickerTimeView setHiddenPickerTimeView:^{
        [MTMaskView hidden];
    }];
}


@end
